package businessTier;

import core.Genre;
import core.Medium;
import core.Movie;
import core.MovieException;
import integrationTier.AbstractDAOFactory;
import integrationTier.IDatabaseDAO;
import integrationTier.IGenreDAO;
import integrationTier.IMediumDAO;
import integrationTier.IMovieDAO;
import java.util.List;

/**
 * Stara se o zpracovani business logiky a predani dat nizsim vrstvam aplikace
 * @author vojta
 */
public class MovieFacade implements IMovieFacade{

    IMovieDAO movieDAO;
    IMediumDAO mediumDAO;
    IDatabaseDAO databaseDAO;
    IGenreDAO genreDAO;
    static String path;

    public MovieFacade(String path) throws MovieException {        
        this.path = path;
        AbstractDAOFactory fact = AbstractDAOFactory.createSQLDAOFactory(path);
        movieDAO = fact.getMovieDAO();
        mediumDAO = fact.getMediumDAO();
        genreDAO = fact.getGenreDAO();
        databaseDAO = fact.getDatabaseDAO();
        movieDAO = fact.getMovieDAO();
    }

    /**
     *
     * vytvori novou instanci tridy MovieFacade
     * @param path cesta k databazi
     * @return instanci tridy MovieFacade
     * @throws MovieException
     */
    public static IMovieFacade createInstance(String path) throws MovieException {
       MovieFacade.path = path;
       return new MovieFacade(path);
    }

    /**
     * Vrati instanci tridy, ktera jiz musela byt predem vytvorena metodou createInstance
     * @return IMovieFacade
     * @throws MovieException
     */
    public static IMovieFacade getInstance() throws MovieException{
        return new MovieFacade(MovieFacade.path);
    }

    public static boolean isInstance() {
        if (path == null)
            return false;
        else
            return true;
    }

    /**
     * vypise vsechny media v databazi
     * @return ArrayList
     * @throws MovieException
     */
    public List<Medium> getAllMedia() throws MovieException {
        return mediumDAO.listAll();
    }

    /**
     * Ziska detaily o jednom konkretnim mediu
     * @param medium
     * @return Medium
     * @throws MovieException
     */
    public Medium getMedium(Medium medium) throws MovieException {
        return mediumDAO.get(medium);
    }

    /**
     * Prida nove medium
     * @param medium
     * @throws MovieException
     */
    public void addMedium(Medium medium) throws MovieException {
        mediumDAO.add(medium);
    }

    /**
     * Odstrani medium
     * @param medium
     * @throws MovieException
     */
    public void removeMedium(Medium medium) throws MovieException {
        mediumDAO.remove(medium);
    }

    /**
     * Zmeni udaje o mediu
     * @param medium
     * @throws MovieException
     */
    public void changeMedium(Medium medium) throws MovieException {
        mediumDAO.change(medium);
    }

    /**
     * Vypise vsechny zanry
     * @return ArrayList
     * @throws MovieException
     */
    public List<Genre> getAllGenres() throws MovieException {
        return genreDAO.listAll();
    }

    /**
     * Ziska informace o danem zanru
     * @param genre
     * @return Genre
     * @throws MovieException
     */
    public Genre getGenre(Genre genre) throws MovieException {
        return genreDAO.get(genre);
    }

    /**
     * Prida zanr
     * @param genre
     * @throws MovieException
     */
    public void addGenre(Genre genre) throws MovieException {
        genreDAO.add(genre);
    }

    /**
     * Odstrani zanr
     * @param genre
     * @throws MovieException
     */
    public void removeGenre(Genre genre) throws MovieException {
        genreDAO.remove(genre);
    }

    /**
     * Zmeni zanr
     * @param genre
     * @throws MovieException
     */
    public void changeGenre(Genre genre) throws MovieException {
        genreDAO.change(genre);
    }

    /**
     * Vypise vsechny filmy
     * @return ArrayList
     * @throws MovieException
     */
    public List<Movie> getAllMovies() throws MovieException {
        return movieDAO.listAll();
    }

    /**
     * Ziska informace o danem filmu
     * @param movie
     * @return Movie
     * @throws MovieException
     */
    public Movie getMovie(Movie movie) throws MovieException {
        return movieDAO.get(movie);
    }
 
    /**
     * Najde vsechny filmy odpovidajici movie
     * @param movie
     * @return ArrayList
     * @throws MovieException
     */
    public List<Movie> findMovies(Movie movie) throws MovieException {
        return movieDAO.find(movie);
    }

    /**
     * PRida film
     * @param movie
     * @throws MovieException
     */
    public void addMovie(Movie movie) throws MovieException {
        movieDAO.add(movie);
    }

    /**
     * Odstrani film
     * @param movie
     * @throws MovieException
     */
    public void removeMovie(Movie movie) throws MovieException {
        movieDAO.remove(movie);
    }

    /**
     * Zmeni film
     * @param movie
     * @throws MovieException
     */
    public void changeMovie(Movie movie) throws MovieException {
        movieDAO.change(movie);
    }

    public void createDatabase() throws MovieException {
        databaseDAO.create();
    }

    /**
     * Ukonci vsechna pripojeni k DB
     * @throws MovieException
     */
    public void close() throws MovieException {
        try {
        movieDAO.close();
        mediumDAO.close();
        databaseDAO.close();
        genreDAO.close();

       } catch (Exception ex) {
        }
    
    }

}